package org.batatasframework.web.ui;

import org.batatasframework.web.element.TableTd;
import org.batatasframework.web.element.TableTr;

public class FormCell {
	
	private int colspan;
	private int rowspan;
	private String headTitle;
	private boolean showHead;
	
	/** 一个项的UI，如：输入框、下拉框，下拉树等。。。 */
	private Component component;
	
	/** 没有抬头的情况 */
	public FormCell(){}
	
	/** 有抬头 */
	public FormCell(String headTitle){
		this.headTitle = headTitle;
	}
	
	public String getKey(){
		return component.getKey();
	}
	
	public int getColspan() {
		return colspan;
	}

	public void setColspan(int colspan) {
		this.colspan = colspan;
	}

	public int getRowspan() {
		return rowspan;
	}

	public void setRowspan(int rowspan) {
		this.rowspan = rowspan;
	}

	public Component getComponent() {
		return component;
	}

	public void setComponent(Component component) {
		this.component = component;
	}
	
	public void render(TableTr tr){
		int tdcolspan = 1;
		if(colspan > 1){
			tdcolspan = colspan * 2 - 1;
		}
		
		if(showHead){
			String title = headTitle;
			if(null == title) title = "";
			
			TableTd head = TableTd.th();
			head.setContent(title);
			head.setRowspan(rowspan);
			tr.addElement(head);
		}else{
			tr.addElement(TableTd.SKIP);
			tdcolspan += 1;
		}
		
		TableTd content = TableTd.td();
		content.setColspan(tdcolspan);
		content.setRowspan(rowspan);
		content.setContent(component);
		tr.addElement(content);
	}
}
